<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>登录页</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
    <link rel="stylesheet" href="common.css" />
    <link rel="stylesheet" href="logIn.css" />
  </head>
  <body>
    <div class="navigation">
      <!-- 头像 -->
      <img
        src="https://img1.baidu.com/it/u=4205447136,2730860147&fm=253&fmt=auto&app=138&f=JPEG?w=300&h=300"
        alt="未登录"
      />
      <div class="title">未登录</div>
    </div>
    <!-- 登录页面 -->

    <div class="login-Container">
      <div class="dialog">
        <h3>登录</h3>

        <div id="fileImage">
          <div>
            <input id="i" type="button" value="" />
          </div>
        </div>

        <div class="row">
          <label for="username">用户名</label>
          <input type="text" id="username" />
        </div>
        <div class="row">
          <label for="password">密码</label>
          <input type="password" id="password" onclick="getImage()" />
        </div>
        <div class="r">
          <input type="submit" id="goreg" value="注册" onclick="goReg()" />
          <input type="submit" id="submit" value="登录" onclick="login()" />
          <input
            type="submit"
            id="getPassword"
            value="忘记密码"
            onclick="toGetPassword()"
          />
        </div>
      </div>
    </div>

    <script>
      function toGetPassword() {
        var name = jQuery("#username");
        var password = jQuery("#password");
        if (name.val().trim() == "") {
          alert("请输入是谁！");
          name.focus();
          return;
        }
        var keyValue = {
          name: name.val(),
          password: password.val(),
        };
        jQuery.ajax({
          type: "POST",
          url: "remind",
          contentType: "application/json; charset=utf8",
          data: JSON.stringify(keyValue),
          success: function (body) {
            if (body != "No") {
              jQuery("#password").val(body);
            } else {
              alert("不存在这个用户！");
            }
          },
        });
      }

      function goReg() {
        window.location.href = "register.html";
      }

      jQuery.ajax({
        type: "get",
        url: "login",
        success: function (body) {
          if (body != "No") {
            window.location.href = "wall.html";
          } else {
            alert("登录失效");
          }
        },
      });
      function login() {
        var name = jQuery("#username");
        var password = jQuery("#password");
        if (name.val().trim() == "") {
          alert("请输入是谁！");
          name.focus();
          return;
        }
        if (password.val().trim() == "") {
          alert("请输入密码！");
          password.focus();
          return;
        }
        var keyValue = {
          name: name.val(),
          password: password.val(),
        };

        jQuery.ajax({
          type: "POST",
          url: "login",
          contentType: "application/json; charset=utf8",
          data: JSON.stringify(keyValue),
          success: function (body) {
            if (body == "No") {
              alert("用户名或者密码错误！");
            } else {
              window.location.href = "wall.html";
            }
          },
        });
      }

      function getImage() {
        var name = jQuery("#username");
        var password = jQuery("#password");

        if (name.val().trim() == "") {
          alert("请输入是谁！");
          name.focus();
          return;
        }
        var keyValue = {
          name: name.val(),
          password: password.val(),
        };
        jQuery.ajax({
          type: "post",
          url: "image",
          contentType: "application/json; charset=utf8",
          data: JSON.stringify(keyValue),
          success: function (body) {
            if (body != "No") {
              jQuery("#i").css("background-image", "url(" + body + ")");
            } else {
              jQuery("#i").css(
                "background-image",
                "url(https://img1.baidu.com/it/u=4205447136,2730860147&fm=253&fmt=auto&app=138&f=JPEG?w=300&h=300)"
              );
            }
          },
        });
      }
    </script>
  </body>
</html>
